﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace KBS8.Presentation.MapView
{
    internal class VisualMapTile : FrameworkElement
    {
        // Create a collection of child visual objects.
        private VisualCollection _children;
        private List<DummyPOI> _poiList; 

        public VisualMapTile(DrawingVisual drawingVisual)
        {
            //Dummy Point Of Interest code
            DummyPOI dpoi = new DummyPOI {Name = "Dummy", Rad = 6, X = 47, Y = 47 };
            _poiList = new List<DummyPOI> {dpoi};
            //End dummy

            _children = new VisualCollection(this) {drawingVisual};

            // Add the event handler for MouseLeftButtonUp and MouseMove. 
            MouseLeftButtonUp += VisualMapTile_MouseLeftButtonUp;
            MouseMove += VisualMapTile_MouseMove;
        }
        /// <summary>
        /// Triggered when the mouse moves over the component, used for POI clickablilty
        /// </summary>
        /// <param name="sender">Invoker</param>
        /// <param name="e">The MouseEventArgs for this event</param>
        void VisualMapTile_MouseMove(object sender, MouseEventArgs e)
        {
            Point pt = e.GetPosition((UIElement)sender);

            foreach (DummyPOI dpoi in _poiList)
            {
                Cursor = dpoi.IsInClickRange(pt) ? Cursors.Hand : Cursors.Arrow;
            }

        }
        /// <summary>
        /// Provide a required override for the VisualChildrenCount property. 
        /// </summary>
        protected override int VisualChildrenCount
        {
            get { return _children.Count; }
        }

        /// <summary>
        /// Provide a required override for the GetVisualChild method.
        /// </summary>
        /// <param name="index"></param>
        ///  <returns></returns>
        protected override Visual GetVisualChild(int index)
        {
            if (index < 0 || index >= _children.Count)
            {
                throw new ArgumentOutOfRangeException();
            }
            return _children[index];
        }


        /// <summary>
        /// Capture the mouse event and hit test the coordinate point value against 
        /// the child visual objects. 
        /// </summary>
        /// <param name="sender">The invoker of the event</param>
        /// <param name="e">The MouseButtonEventArgs for the event</param>
        void VisualMapTile_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // Retreive the coordinates of the mouse button event.
            Point pt = e.GetPosition((UIElement)sender);

            foreach (DummyPOI dpoi in _poiList)
            {
                if (dpoi.IsInClickRange(pt))
                    MessageBox.Show(dpoi.Name);
            }
            // Initiate the hit test by setting up a hit test result callback method.
            VisualTreeHelper.HitTest(this, null, Callback, new PointHitTestParameters(pt));
        }

        /// <summary>
        /// If a child visual object is hit, toggle its opacity to visually indicate a hit. 
        /// </summary>
        /// <param name="result">The HitTestResult</param>
        /// <returns></returns>
        private HitTestResultBehavior Callback(HitTestResult result)
        {
            if (result.VisualHit.GetType() == typeof(DrawingVisual))
            {
                DrawingVisual dv = (DrawingVisual) result.VisualHit;

                /*double currentRotation = 0;
                if (dv.Transform != null)
                    currentRotation = ((RotateTransform) dv.Transform).Angle;
                
                dv.Transform = new RotateTransform(currentRotation + 90 , (dv.Drawing.Bounds.Right - dv.Drawing.Bounds.Left) / 2, (dv.Drawing.Bounds.Bottom - dv.Drawing.Bounds.Top) / 2);*/
            }

            // Stop the hit test enumeration of objects in the visual tree. 
            return HitTestResultBehavior.Stop;
        }
    }
    class DummyPOI
    {
        public int X { get; set; }
        public int Y { get; set; }
        public int Rad { get; set; }
        public string Name { get; set; }

        public bool IsInClickRange(Point mouseLocation)
        {
            return mouseLocation.X > X - Rad && mouseLocation.X < X + Rad && mouseLocation.Y > Y - Rad &&
                   mouseLocation.Y < Y + Rad;
        }
    }
}
